home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / FDF101.ARJ / ELIB.ZOO / dirs.c < prev    next >
C/C++ Source or Header  |  1992-04-30  |  2KB  |  101 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <dir.h>
  5. #include <ctype.h>
  6.  
  7. #include "elib.h"
  8.  
  9.  
  10.  
  11. /*
  12.  * is_special
  13.  *
  14.  * given a directory name, return non-zero if it is 'special' (i.e. '.' or
  15.  * '..') or zero if it is normal
  16.  */
  17. int is_special(char *d_name)
  18.  
  19. {
  20.     int d_len;
  21.  
  22.     if ((d_name == NULL) || ((d_len = strlen(d_name)) > 2))
  23.         return 0;
  24.     else if (d_len == 2)
  25.         return (!strcmp(d_name, ".."));
  26.     else if (d_len == 1)
  27.         return (d_name[0] == '.');
  28.     else    /* null directory name */
  29.         return 0;
  30. }
  31.  
  32.  
  33. /*
  34.  * append_dir_to_path
  35.  *
  36.  * given a path and a directory name, return a pointer to memory with
  37.  * the directory appended to the path
  38.  */
  39.  
  40.  
  41. char *append_dir_to_path(char *path, char *dir)
  42.  
  43. {
  44.     static char *dir_sep = PATH_SEPARATOR;
  45.     char *ret_val, sep_byte;
  46.     int path_len, mem_nec;
  47.  
  48.     mem_nec = (path_len = strlen(path)) +
  49.               (sep_byte = ((path[path_len-1] != dir_sep[0]) ? 1 : 0)) +
  50.               strlen(dir) + 1;
  51.     if ((ret_val = malloc((size_t) mem_nec)) == NULL) {
  52.         printf("append_dir_to_path: memory allocation failure!\n");
  53.         exit(-1);
  54.     }
  55.     else {
  56.         strcpy(ret_val, path);
  57.         if (sep_byte)
  58.             strcat(ret_val, dir_sep);
  59.         strcat(ret_val, dir);
  60.  
  61.         return ret_val;
  62.     }
  63. }
  64.  
  65.  
  66. /*
  67.  *    format_dir()
  68.  *
  69.  *    Input:
  70.  *        dir - the directory as specified by user
  71.  *    Output:
  72.  *        formatted_dir - a formatted version of 'dir'
  73.  */
  74.  
  75. void format_dir(char *dir, char app_slash, char *formatted_dir)
  76. {
  77.     char cur_dir[MAXPATH];
  78.     char other_cwd[MAXPATH];
  79.  
  80.  
  81.     getcwd(cur_dir, MAXPATH);
  82.     if (strlen(dir) > 1){
  83.         if (*(dir + 1) == ':'){
  84.             if (isupper(*dir))
  85.                 tolower(*dir);
  86.             setdisk(*dir - 'a');
  87.         }
  88.     }
  89.     getcwd(other_cwd, MAXPATH);
  90.     chdir(dir);
  91.     getcwd(formatted_dir, MAXPATH);
  92.  
  93.     chdir(other_cwd); /* return to cwd of disk to be formatted */
  94.  
  95.     if ((app_slash) &&
  96.         (*(formatted_dir + strlen(formatted_dir) - 1) != '\\'))
  97.         strcat(formatted_dir, "\\");
  98.     setdisk(*cur_dir - 'A');
  99.     chdir(cur_dir);
  100. }
  101.